﻿using Newtonsoft.Json.Linq;

using System.Data;

namespace System
{
    /// <summary>
    /// 列名注解
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class ColumnAttribute : Attribute
    {
        public ColumnAttribute(int length = 0)
        {
            Length = length;
        }

        public ColumnAttribute(bool canNull)
        {
            CanNull = canNull;
        }
        public ColumnAttribute(int length, bool canNull)
        {
            CanNull = canNull;
            Length = length;
        }

        public void Cost(Func<object, object> func, Action<object, object> setAction, string name, Type type)
        {
            this.GetValue = func;
            this.SetValue = setAction;
            this.PropertyType = type;
            this.PropertyName = name;
            if (!this.FieldType.HasValue)
            {
                if (typeof(JToken).IsAssignableFrom(type))
                {
                    this.FieldType = DbType.String;
                }
                else
                {
                    this.FieldType = (DbType)Enum.Parse(typeof(DbType), type.Name);
                }
            }
            if (this.Digit <= 0)
            {
                this.Digit = this.FieldType switch
                {
                    DbType.Decimal => 2,
                    DbType.Double => 3,
                    DbType.Single => 2,
                    _ => 0,
                };
            }
            if (this.Length <= 0)
            {
                this.Length = this.FieldType switch
                {
                    DbType.Int64 => 18,
                    DbType.Int32 => 11,
                    DbType.Int16 => 6,
                    DbType.Byte => 4,
                    DbType.Boolean => 1,
                    DbType.Decimal => 18,
                    DbType.Double => 13,
                    DbType.Single => 6,
                    DbType.String => 255,
                    _ => 0,
                };
            }
            if (this is PrimaryAttribute primary)
            {
                if (primary.PrimaryType == PrimaryType.Default && !primary.Automatic)
                {
                    primary.PrimaryType = this.FieldType switch
                    {
                        DbType.Int32 => PrimaryType.TimeTicks,
                        DbType.Int64 => PrimaryType.TimeTicks,
                        DbType.String => PrimaryType.Guid,
                        _ => PrimaryType.None,
                    };
                }

            }
        }
        public Func<object, object> GetValue { get; private set; }

        public Action<object, object> SetValue { get; private set; }

        public Type PropertyType { get; private set; }

        public DbType? FieldType { get; private set; }
        /// <summary>
        /// 字段名称
        /// </summary>
        public string PropertyName { get; private set; }
        /// <summary>
        /// 列名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 可为空
        /// </summary>
        public bool CanNull { get; set; }

        /// <summary>
        /// 长度
        /// </summary>
        public int Length { get; set; }

        /// <summary>
        /// 小时位数
        /// </summary>
        public int Digit { get; set; }


        /// <summary>
        /// 自动增长
        /// </summary>
        public bool Automatic { get; set; }

        /// <summary>
        /// 字符集
        /// </summary>
        public string Character { get; set; } = "utf-8";

        public object DefaultValue { get; set; }


    }
}
